<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 深拷贝:拷贝出来的目标对象的和原目标对象,相互之间互不影响.修改其中一个对象,两外一个对象不会进行影响
        var obj = {
            name: '张美丽',
            age: 20,
            hobby: ['运动', '敲代码', '谈恋爱'],
            school: {
                id: 'y001',
                sname: '姚班'
            },
            sayHi: function() {
                console.log('函数');
            },
            rp: /abc/
        };
        var copyObj = {};

        // JSON.stringify(obj):将对象转换成JSON字符串
        console.log(JSON.stringify(obj));
        // JSON.parse(str):将JSON字符串转换为对象
        console.log(JSON.parse('{"name":"张美丽","age":20,"hobby":["运动","敲代码","谈恋爱"],"school":{"id":"y001","sname":"姚班"}}'));
        // 进行深拷贝
        copyObj = JSON.parse(JSON.stringify(obj));
        // 修改拷贝后的对象
        // copyObj.hobby[0] = '读书';
        // copyObj.school.id = 'y002';
        console.log(copyObj);
        console.log(obj);
        // 缺点:对个别复杂数据类型没办法友好的拷贝
    </script>
</body>

</html>